'use strict';

/* Controllers */
app.controller('houseOwnerRentAmountListCtl', function($scope,$compile,ngDialog,uiLoad,$state) {
    var data = angular.copy($scope.ngDialogData, {});
    var rentTypeObj = {
        0: "年付",
        1: "季付",
        2: "月付"
    };
    var applyTypeObj = {
        0: "支付包",
        1: "微信",
        2: "现金"
    };
    $scope.checkType = {};
    $scope.houseType = {}
    $scope.planStatus = {};
    $scope.checkTypeArray = [{ "name": "全部", "value": "-1" }, { "name": "未交租", "value": "0" }, { "name": "已交租", "value": "1" }];
    if($scope.queryParams){

    }
    $scope.queryParams = {
        'planStatus': $scope.planStatus.selected,
        'leaseStartTime': $scope.leaseStartTime,
        'leaseEndTime': $scope.leaseEndTime,
        'nameCode': $scope.nameCode
    }
    $scope.$watch('_contractNo', function(newValue, oldValue) {
         $scope.queryParams.contractNo = newValue;
    })
    $scope.$watch('_tenantsName', function(newValue, oldValue) {
         $scope.queryParams.tenantsName = newValue;
    })
    $scope.$watch('_roomCode', function(newValue, oldValue) {
         $scope.queryParams.roomCode = newValue;
    })
    $scope.$on('contractDate.start', function(event, data) {
        $scope.queryParams.leaseStartTime = data ? data : "";
    })
    $scope.$on('contractDate.end', function(event, data) {
        $scope.queryParams.leaseEndTime = data ? data : "";
    })
    $scope.queryUser = function(){
        if($scope.queryParams.planStatus && $scope.queryParams.planStatus.selected){
            var planStatusCache = $scope.queryParams.planStatus.selected;
            if($scope.queryParams.planStatus.selected){
                $scope.queryParams.planStatus = $scope.queryParams.planStatus.selected.value
            }
            $scope.dataTable.fnClearTable();
            $scope.queryParams.planStatus = {};
            $scope.queryParams.planStatus.selected = {};
            $scope.queryParams.planStatus.selected = planStatusCache;
        }else{
            $scope.dataTable.fnClearTable();
        }
    };
    $scope.tableLoanDatas = {
        "sAjaxSource": "1031",
        "aoColumns":  [
            {
                "mDataProp": "tenantsName",
                "sTitle": "租客姓名",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "areaName",
                "sTitle": "小区名称",
                "sDefaultContent": "",
                "sClass": "center"
            },{
                "mDataProp": "roomCode",
                "sTitle": "房间号",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "rentCycle",
                "sWidth": "260px",
                "sTitle": "租赁周期",
                "sDefaultContent": "",
                "sClass": "center rent-circle"
            },{
                "mDataProp": "contractNo",
                "sWidth": "240px",
                "sTitle": "合同编号",
                "sDefaultContent": "",
                "sClass": "center"
            },  
            // {
            //     "mDataProp": "planRepaymentAmount",
            //     "sTitle": "实际租金",
            //     "sDefaultContent": "",
            //     "sClass": "center"
            // }, {
            //     "mDataProp": "payTypeName",
            //     "sTitle": "支付方式",
            //     "sDefaultContent": "",
            //     "sClass": "center"
            // }, 
            {
                "mDataProp": "",
                "sTitle": "交租情况",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "",
                "sTitle": "超期情况",
                "sDefaultContent": "",
                "sClass": "center"
            }, 
            // {
            //     "mDataProp": "planStatus",
            //     "sTitle": "当期交租状态",
            //     "sDefaultContent": "",
            //     "sClass": "center check-status"
            // }, 
            {
                "mDataProp": "operate",
                "sTitle": "操作",
                "sDefaultContent": "",
                "sClass": "center handle-td"
            }
        ],
        "fnRowCallback": function(nRow, aData, iDisplayIndex ) {

          var btnStr = "";
          btnStr += "<a href='javascript:;' class='btn btn-yellow' ng-click='godetail(\"" + aData.id + "\")' style='margin-right:10px;'>查看</a>";
          var status = "";
          if (aData.planStatus) {
            var status = "已付款";
          }else{
            var status = "未付款";
          }
          // console.log(aData);
          function time(mydate){
              // console.log(mydate);
              var str = mydate.substring(0,10);
              var dataArr = str.split('-');
              var timeStr = dataArr[0]
                +"年"+dataArr[1]+"月"+dataArr[2]
                +"日";
                return timeStr;
            }

            var leaseCycle = time(aData.leaseStartTime) + " - " + time(aData.leaseEndTime);
            $(nRow).find(".rent-circle").empty().append(leaseCycle);
            $(nRow).find(".handle-td").append($compile(btnStr)($scope));
            $(nRow).find(".check-status").empty().append(status);
        },
        "fnDrawCallback": function(oSettings) {
            $scope.planStatusValue = $scope.queryParams.planStatus;
            if ("number" == typeof $scope.planStatusValue) {
                $scope.checkTypeArray.map(function(ele, index) {
                    if ($scope.planStatusValue == ele.value) {
                        $scope.planStatus.selected = ele;
                    }
                })
            }
        },
        "fnServerParams": function(aoData) {
            if($scope.planStatus && $scope.planStatus.selected){
                $scope.queryParams.planStatus = $scope.planStatus.selected.value
            }
            aoData.push({
                'name': 'queryParams',
                'value': $scope.queryParams
            });
            return aoData;
        }
    };

    $scope.godetail = function (orderId) {
      ngDialog.openConfirm({
          template: 'views/houseOwner/rentRepayDetail.html',
          resolve: {
            load: function() {
              return uiLoad.load(['js/controllers/houseOwner/rentRepayDetail.js'])
            }
          },
          data:{
            orderId:orderId
          },
          scope: $scope,
          width: 1200,
          // height: 700
        }).then(function(data) {

        }, function(reason) {

        });

    }

    $scope.dataTable = undefined;

    // 回调函数,返回实例化的jquery 插件对象
    $scope.initFinish = function(dataTable) {
        $scope.dataTable = dataTable;
    }
    $('#example thead').on('click', 'th', function() {
        // var data = $('#example').DataTable().row( this ).data();
        // alert('You clicked on  row');
    });
    $('#example tbody').on('click', 'span#edit', function() {
        var data = $('#example').DataTable().row($(this).parents('tr'));
        alert("查看修改：" + data[0]);
    });
    $('#example tbody').on('click', 'span#del', function() {
        // var data = $('#example').DataTable().row($(this).parents('tr')).data();
    });
});
    


app.controller('ContractTimeCtrl',function($scope){
  $scope.today = function() {
      $scope.leaseStartTime = "";
      $scope.leaseEndTime = "";
    };

    $scope.closeAll = function() {
      $scope.openContractBeginDated =
      $scope.openContractEndDated = false;
    }

    $scope.openContractBeginDate = function($event) {
      $event.preventDefault();$event.stopPropagation();$scope.closeAll();
      $scope.openContractBeginDated = true;
    };
    $scope.openContractEndDate = function($event) {
      $event.preventDefault();$event.stopPropagation();$scope.closeAll();
      $scope.openContractEndDated = true;
    };

    $scope.dateOptions = {
      format: 'yyyy/MM/dd',
      startingDay: 1,
      class: 'datepicker'
    };

    $scope.format = 'yyyy/MM/dd';

    $scope.today();

    $scope.$watch('leaseStartTime',function(newValue,oldValue){
      if("[object Date]" === Object.prototype.toString.call(newValue)) {
         $scope.$emit('contractDate.start',moment(newValue).format('YYYY/MM/DD'))
      } else if (null === newValue) {
        $scope.$emit('contractDate.start','')
      }
    });
    $scope.$watch('leaseEndTime',function(newValue,oldValue){
      if("[object Date]" === Object.prototype.toString.call(newValue)) {
         $scope.$emit('contractDate.end',moment(newValue).format('YYYY/MM/DD'))
      } else if (null === newValue) {
        $scope.$emit('contractDate.end','')
      }
    });
});
